package org.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.demo.constant.MessageConstant;
import org.example.demo.entity.CultivateLog;
import org.example.demo.service.CultivateLogService;
import org.example.demo.service.InformationHintService;
import org.example.demo.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 培训记录控制器
 */
@RestController
@RequestMapping("cultivateLog")
public class CultivateLogController {

    @Resource
    private CultivateLogService cultivateLogService;

    @Autowired
    private InformationHintService informationHintService;

    /**
     * 分页查询培训记录
     * 
     * @param cultivateLog 查询条件
     * @param pageNum      页码
     * @param pageSize     每页大小
     * @return 查询结果
     */
    @PostMapping("page")
    public AjaxResult page(@RequestBody CultivateLog cultivateLog,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<CultivateLog> page = cultivateLogService.selectPage(cultivateLog, pageNum, pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("page", page);
        result.put("total", page.getTotal());

        return AjaxResult.success(result);
    }

    /**
     * 根据ID查询培训记录
     * 
     * @param id 培训记录ID
     * @return 查询结果
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id) {
        CultivateLog cultivateLog = cultivateLogService.getById(id);

        if (cultivateLog != null) {
            return AjaxResult.success(cultivateLog);
        } else {
            return AjaxResult.error("培训记录不存在");
        }
    }

    /**
     * 新增培训记录
     * 
     * @param cultivateLog 培训记录信息
     * @return 操作结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody CultivateLog cultivateLog) {
        if (cultivateLogService.save(cultivateLog)) {


            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 修改培训记录
     * 
     * @param cultivateLog 培训记录信息
     * @return 操作结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody CultivateLog cultivateLog) {
        if (cultivateLogService.updateById(cultivateLog)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 删除培训记录
     * 
     * @param id 培训记录ID
     * @return 操作结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        if (cultivateLogService.removeById(id)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据培训类型查询培训记录列表
     *
     * @param cultivateType 培训类型
     * @param pageNum       页码
     * @param pageSize      每页大小
     * @return 查询结果
     */
    @GetMapping("type/{cultivateType}")
    public AjaxResult getByCultivateType(@PathVariable Integer cultivateType,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<CultivateLog> page = cultivateLogService.getByCultivateType(cultivateType, pageNum, pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("page", page);
        result.put("total", page.getTotal());

        return AjaxResult.success(result);
    }

    /**
     * 根据培训状态查询培训记录列表
     *
     * @param cultivateStatus 培训状态
     * @param pageNum         页码
     * @param pageSize        每页大小
     * @return 查询结果
     */
    @GetMapping("status/{cultivateStatus}")
    public AjaxResult getByCultivateStatus(@PathVariable Integer cultivateStatus,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<CultivateLog> page = cultivateLogService.getByCultivateStatus(cultivateStatus, pageNum, pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("page", page);
        result.put("total", page.getTotal());

        return AjaxResult.success(result);
    }
}